
package doc2db.interfaces.action.open;

import doc2db.Launcher;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterException;
import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.JTextComponent;

/**
 *
 * @author yuriy
 */
public class JEditPContentMenu  implements HyperlinkListener{
    String hyperLinkName = "__NULL";
    private MEditPane edit;
    private Font font = new Font(Font.MONOSPACED, Font.PLAIN, 12);

    public JEditPContentMenu (MEditPane __p){
        edit = __p;
    }

    public JPopupMenu getLynkSelectPaneMenu(final JTextComponent pane){
        JPopupMenu m = new JPopupMenu();
        String path = "menu/editor/";
        
        JMenuItem cut = new JMenuItem(new AbstractAction("Вырезать",
                Launcher.getIcon(path + "cut.png")) {
            @Override public void actionPerformed(ActionEvent e) {
                pane.cut();
            }
        });
        cut.setFont(font);
        m.add(cut);
        
        JMenuItem copy = new JMenuItem(new AbstractAction("Копировать",
                Launcher.getIcon(path + "copy.png")) {
            @Override public void actionPerformed(ActionEvent e) {
                pane.copy();
            }
        });
        copy.setFont(font);
        m.add(copy);
        
        JMenuItem paste = new JMenuItem(new AbstractAction("Вставить",
                Launcher.getIcon(path + "paste.png")) {
            @Override public void actionPerformed(ActionEvent e) {
                pane.copy();
            }
        });
        paste.setFont(font);
        m.add(paste);

        m.addSeparator();

        JMenuItem selectAll = new JMenuItem(new AbstractAction("Выделить всё",
                Launcher.getIcon(path + "selectAll.png")) {
            @Override public void actionPerformed(ActionEvent e) {
                pane.selectAll();
            }
        });
        selectAll.setFont(font);
        m.add(selectAll);

        JMenuItem select = new JMenuItem(new AbstractAction("Очистить",
                Launcher.getIcon(path + "delete.png")) {
            @Override public void actionPerformed(ActionEvent e) {
                pane.setVisible(false);
            }
        });
        select.setFont(font);
        //m.add(select);

        m.addSeparator();

        JMenuItem print = new JMenuItem(new AbstractAction("Печять",
                Launcher.getIcon("menu/Printer.png")) {
            @Override public void actionPerformed(ActionEvent e) {
                try {
                    pane.print();
                } catch (PrinterException ex) {
                    JOptionPane.showMessageDialog(pane, ex.getMessage());
                    System.err.println(ex);
                }
            }
        });
        print.setFont(font);
        m.add(print);

        return m;
    }

    public JPopupMenu getLynkMenu(){
        JPopupMenu m = new JPopupMenu();
        
        JMenuItem select = new JMenuItem(new AbstractAction("Показать") {
            @Override public void actionPerformed(ActionEvent e) {
                if (!hyperLinkName.equals("__NULL")) {
                    int e1 = 0;
                    try {
                        e1 = Integer.parseInt(hyperLinkName);
                        edit.open(e1);
                    } catch (NumberFormatException numberFormatException) {
                        System.err.println(e.getActionCommand());
                    }
                }
                hyperLinkName = "__NULL";
            }
        });
        select.setFont(font);
        m.add(select);

        JMenuItem drop = new JMenuItem(new AbstractAction("Удалить") {
            @Override public void actionPerformed(ActionEvent e) {
                if (!hyperLinkName.equals("__NULL")) {

                }
                hyperLinkName = "__NULL";
            }
        });
        drop.setFont(font);
        m.add(drop);

        return m;
    }


    @Override public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
            edit.setIsOnHyperLink(true);
            edit.setHyperLinkName("" + e.getDescription());
        }
        if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
            edit.setIsOnHyperLink(false);
            edit.setHyperLinkName("__NULL");
        }
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            if (!edit.isIsFile()) {
                int e1 = 0;
                try {
                    e1 = Integer.parseInt(e.getDescription());
                    edit.open(e1);
                } catch (NumberFormatException numberFormatException) {
                    System.err.println(e.getDescription());
                }
            } else {
            }
        }
    }

}
